package framework;

import java.util.Vector;
/**
 * 生成应用菜单
 * @author host
 *
 */
public class JGMeun {
	com.PublicSystem sys = com.PublicSystem.getInstance();	
	StringBuffer sb = new StringBuffer();	
	/**
	 * 获取菜单
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	public String getMenu(String userid) throws Exception {			
		String bmid;
		String sjbmid;
		String bmmc;
		String url;			
		Vector res = menuPid(userid);
		if(res != null) {
			for(int i = 0; i < res.size(); i++) {		
				bmid = (String) ((Vector) res.elementAt(i)).elementAt(0);
				bmmc = (String) ((Vector) res.elementAt(i)).elementAt(1);
				sb.append("<li><span expanded=false>" + bmmc + "</span><ul><li>");
				Vector res1 = menuSid(bmid, userid);
				if(res1 != null) {				
					getData(res1, userid, bmid);
				}
				sb.append("</ul></li>");
			}
		}		
		return sb.toString();
	}
	/**
	 * 获取数据
	 * @param vector
	 * @param userid
	 * @param bmid
	 * @throws Exception
	 */
	private void getData(Vector vector, String  userid, String bmid)
	throws Exception {
		String id = "";
		String bmmc = "";
		Vector res2 = menuSid(bmid, userid);
		for (int i = 0; i < res2.size(); i++) {		
			bmid = (String) ((Vector) res2.elementAt(i)).elementAt(0);
			bmmc = (String) ((Vector) res2.elementAt(i)).elementAt(1);		
			Vector resChild = menuSid(bmid, userid);
			if (resChild != null) {
				String f = sb.substring(sb.length() - 4, sb.length());
				if("<li>".equals(f)){
					sb.append("<span expanded='false'>" + bmmc + "</span>");
				} else {
					sb.append("<li><span expanded='false'>" + bmmc + "</span>");
				}
				sb.append("<ul><li>");
				getData(resChild, userid, bmid);
				sb.append("</li></ul></li>");
			}
		}
    }	
	/**
	 * 获取子菜单pid
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	public Vector menuPid(String userid) throws Exception {
		Vector res = sys.SelResultVec("select bmid,bmmc,sjbmid,bmdj from oa_bmjgb a where bmid=sjbmid " );
		return res;
	}
    /**
     * 获取子菜单sid
     * @param bmid
     * @param userid
     * @return
     * @throws Exception
     */
	public Vector menuSid(String bmid, String userid) throws Exception {
		Vector res = sys.SelResultVec("select c.bmid,c.bmmc,c.sjbmid,c.bmdj from oa_bmjgb c where  sjbmid='" 
	                 + bmid + "' and bmid !='" + bmid + "'" );
		if (res == null) {
			return null;
		} else {
			return res;
		}
	}
}